using System;
using System.ComponentModel;
using System.Windows.Forms;
using FlickrShots.Framework;
using FlickrShots.Framework.Windows;

namespace FlickrShots.GUI.controls
{
	public class SettingsControl : UserControl
    {
        private GroupBox groupBox2;
        private Label label3;
        private GroupBox groupBox1;
        private NumericUpDown refreshTime;
        private ComboBox positionDropDown;
        private CheckBox enableAutomaticRefresh;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private Container components = null;

		public SettingsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

            Initialize(Preferences.Instance);
		}

	    private void Initialize(Preferences preferences)
	    {
            positionDropDown.Items.Add("Center");
            positionDropDown.Items.Add("Tile");
            positionDropDown.Items.Add("Stretch");
            positionDropDown.Items.Add("Max");
            positionDropDown.SelectedIndex = 0;
	    }

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label3 = new System.Windows.Forms.Label();
            this.refreshTime = new System.Windows.Forms.NumericUpDown();
            this.enableAutomaticRefresh = new System.Windows.Forms.CheckBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.positionDropDown = new System.Windows.Forms.ComboBox();
            this.groupBox2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.refreshTime)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.label3);
            this.groupBox2.Controls.Add(this.refreshTime);
            this.groupBox2.Controls.Add(this.enableAutomaticRefresh);
            this.groupBox2.Location = new System.Drawing.Point(144, 8);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(128, 80);
            this.groupBox2.TabIndex = 8;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Auto Refresh";
            // 
            // label3
            // 
            this.label3.Enabled = false;
            this.label3.Location = new System.Drawing.Point(72, 48);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(48, 16);
            this.label3.TabIndex = 3;
            this.label3.Text = "minutes";
            // 
            // refreshTime
            // 
            this.refreshTime.Enabled = false;
            this.refreshTime.Location = new System.Drawing.Point(16, 48);
            this.refreshTime.Maximum = new decimal(new int[] {
            999999,
            0,
            0,
            0});
            this.refreshTime.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.refreshTime.Name = "refreshTime";
            this.refreshTime.Size = new System.Drawing.Size(56, 20);
            this.refreshTime.TabIndex = 2;
            this.refreshTime.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.refreshTime.ValueChanged += new System.EventHandler(this.refreshTime_ValueChanged);
            // 
            // enableAutomaticRefresh
            // 
            this.enableAutomaticRefresh.Location = new System.Drawing.Point(16, 16);
            this.enableAutomaticRefresh.Name = "enableAutomaticRefresh";
            this.enableAutomaticRefresh.Size = new System.Drawing.Size(64, 24);
            this.enableAutomaticRefresh.TabIndex = 4;
            this.enableAutomaticRefresh.Text = "Enable";
            this.enableAutomaticRefresh.CheckedChanged += new System.EventHandler(this.enableAutomaticRefresh_CheckedChanged);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.positionDropDown);
            this.groupBox1.Location = new System.Drawing.Point(8, 8);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(128, 80);
            this.groupBox1.TabIndex = 7;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Position";
            // 
            // positionDropDown
            // 
            this.positionDropDown.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.positionDropDown.Location = new System.Drawing.Point(16, 24);
            this.positionDropDown.Name = "positionDropDown";
            this.positionDropDown.Size = new System.Drawing.Size(104, 21);
            this.positionDropDown.Sorted = true;
            this.positionDropDown.TabIndex = 0;
            this.positionDropDown.SelectedIndexChanged += new System.EventHandler(this.positionDropDown_SelectedIndexChanged);
            // 
            // SettingsControl
            // 
            this.AutoScroll = true;
            this.AutoScrollMinSize = new System.Drawing.Size(264, 264);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "SettingsControl";
            this.Size = new System.Drawing.Size(284, 295);
            this.groupBox2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.refreshTime)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
		#endregion

        private void enableAutomaticRefresh_CheckedChanged(object sender, EventArgs e)
        {
            refreshTime.Enabled = enableAutomaticRefresh.Checked;
            Preferences.Instance.AutoRefreshRate = !refreshTime.Enabled ? TimeSpan.Zero : new TimeSpan(0, (int)refreshTime.Value, 0);
        }

        private void refreshTime_ValueChanged(object sender, EventArgs e)
        {
            Preferences.Instance.AutoRefreshRate = new TimeSpan(0, (int)refreshTime.Value, 0);
        }

        private void positionDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedItem = ((ComboBox)sender).SelectedItem.ToString();
            if (selectedItem == "Center")
            {
                Preferences.Instance.WallpaperPosition = WPSTYLE.CENTER;
            }
            else if(selectedItem == "Tile")
            {
                Preferences.Instance.WallpaperPosition = WPSTYLE.TILE;    
            }
            else if(selectedItem == "Max")
            {
                Preferences.Instance.WallpaperPosition = WPSTYLE.MAX;
            }
            else if(selectedItem == "Stretch")
            {
                Preferences.Instance.WallpaperPosition = WPSTYLE.STRETCH;
            }
        }
	}
}
